package cn.com.sbell.vss.commander;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.com.sbell.vss.IProtectedCommander;
import cn.com.sbell.vss.Response;
import cn.com.sbell.vss.Util;
import cn.com.sbell.vss.VissSession;
import cn.com.sbell.vss.VssConstants;

/**
 * @author Yifu.HUANG
 * 
 */
public class KeepAlive implements IProtectedCommander {
	private static final Log log = LogFactory.getLog(KeepAlive.class);

	/**
	 * (non-Javadoc)
	 * 
	 * @see cn.com.sbell.vss.ICommander#execute(javax.servlet.http.HttpServletRequest)
	 */
	public Response execute(HttpServletRequest request) {
		// prepare response (include parameters)
		Response response = Response.createResponse(request);
		String s = Util.getTrimParameter(request, "updateAlarmPort");
		if (Util.strIsNull(s) || "true".equalsIgnoreCase(s)) {
			VissSession data = (VissSession) request
					.getAttribute(VssConstants.KEY_USER_SESSION_DATA);
			if (data != null) {
				int ap = request.getRemotePort();
				if (log.isDebugEnabled()) {
					log.debug("Update alarm port to " + ap + " for user "
							+ data.getUsername());
				}
				data.setAlarmPort(ap);
			}
		}
		return response;
	}
}
